//
//  MSGetCoinViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/30.
//  Copyright © 2016年 Mike. All rights reserved.
//

#define btnWidth      90

#import "MSGetCoinViewController.h"
#import "MSGetCoinTableViewController.h"

#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"

@interface MSGetCoinViewController ()
@property (nonatomic, strong) MSGetCoinTableViewController *tableVC;
@end

@implementation MSGetCoinViewController

- (void)initUI{
    self.tableVC = [[MSGetCoinTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.view.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = NO;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.scrollEnabled = NO;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableVC.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableVC.view];
    
    [self layoutHeaderView];
    
    UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-60, 20, 40, 40)];
    closeBtn.titleLabel.font = HeitiFont(15);
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeBtn];
}

- (void)closeClick{
    [rootNavigation popViewControllerAnimated:NO];
}

- (void)layoutHeaderView{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    headerView.backgroundColor = [UIColor whiteColor];
    self.tableVC.tableView.tableHeaderView = headerView;
    
    UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake((SCREEN_WIDTH-140)/2, 70, 140, 0.6)];
    lineLabel.backgroundColor = kLightBlackColor;
    [headerView addSubview:lineLabel];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake((SCREEN_WIDTH-100)/2, lineLabel.bottom-15, 100, 30);
    label.text = @"获取江湖币";
    label.textColor = kLightBlackColor;
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:label];
}

- (void)selectBtnWithIndex:(NSInteger)btnIndex{
    if (btnIndex == 10) {
        MSReleaseTradeViewController *vc = [[MSReleaseTradeViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
        return;
    }
    if (btnIndex == 11) {
        MSInviteFriendsViewController *vc = [[MSInviteFriendsViewController alloc] init];
        [rootNavigation pushViewController:vc animated:YES];
        return;
    }
    return;
    __weak typeof(self)weakSelf = self;
    //分享
    CGFloat imageWidth = 200;
    UIImage *newImage = [self imageCompressForWidth:[UIImage imageNamed:@"tarbar_home_pre"] targetWidth:imageWidth];
    NSData *imageData = UIImagePNGRepresentation(newImage);
    //    NSLog(@"原分享图片->%lu KB",imageData.length/1024);
    while (imageData.length/1024 >= 31) {
        imageWidth -= 10;
        newImage = [self imageCompressForWidth:[UIImage imageNamed:@"tarbar_home_pre"] targetWidth:imageWidth];
        imageData = UIImagePNGRepresentation(newImage);
    }
    if (btnIndex == 102) {//新浪
        //将在新浪微博注册的应用appkey、redirectURL替换下面参数，并在info.plist的URL Scheme中相应添加wb+appkey，如"wb3921700954"，详情请参考官方文档。
        //打开新浪微博的SSO开关，设置新浪微博回调地址，这里必须要和你在新浪微博后台设置的回调地址一致。若在新浪后台设置我们的回调地址，“http://sns.whalecloud.com/sina2/callback”，这里可以传nil
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Sina appKey:sinaAppKey appSecret:sinaAppSecret redirectURL:@"http://sns.whalecloud.com/sina2/callback"];
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"测试分享" descr:[NSString stringWithFormat:@"%@",@"描述副标题"] thumImage:imageData];
        //设置网页地址
        shareObject.webpageUrl = @"http://m.weshare12.com";
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Sina messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    NSLog(@"weibo分享成功！");
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }
    if (btnIndex == 100) {//微信
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:wechatAppID appSecret:wechatAppSecret redirectURL:nil];
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:@"测试分享" descr:[NSString stringWithFormat:@"%@\n%@",@"描述副标题",@"描述副标题"] thumImage:imageData];
        //设置网页地址
        shareObject.webpageUrl = @"http://m.weshare12.com";
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatSession messageObject:messageObject currentViewController:weakSelf completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    NSLog(@"wechat分享成功！");
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }
    if (btnIndex == 101) {//微信圈
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatTimeLine appKey:wechatAppID  appSecret:wechatAppSecret redirectURL:nil];
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[NSString stringWithFormat:@"%@ %@",@"测试分享",@"测试分享"] descr:[NSString stringWithFormat:@"%@ %@",@"测试分享",@"测试分享"] thumImage:imageData];
        //设置网页地址
        shareObject.webpageUrl = @"http://m.weshare12.com";
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_WechatTimeLine messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    NSLog(@"wechat朋友圈成功！");
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }
    if (btnIndex == 103) {//QQ
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_QQ appKey:QQAppKey appSecret:QQAppSecret redirectURL:nil];
        //设置支持没有客户端情况下使用SSO授权
        [[UMSocialQQHandler defaultManager] setSupportWebView:YES];
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[NSString stringWithFormat:@"%@",@"测试分享"] descr:[NSString stringWithFormat:@"%@",@"测试分享"] thumImage:imageData];
        //设置网页地址
        shareObject.webpageUrl = @"http://m.weshare12.com";
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_QQ messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    NSLog(@"QQ分享成功！");
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }
    if (btnIndex == 104) {//QQ空间
        [[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_Qzone appKey:QQAppKey appSecret:QQAppSecret redirectURL:nil];
        //设置支持没有客户端情况下使用SSO授权
        [[UMSocialQQHandler defaultManager] setSupportWebView:YES];
        //创建分享消息对象
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        //创建网页内容对象
        UMShareWebpageObject *shareObject = [UMShareWebpageObject shareObjectWithTitle:[NSString stringWithFormat:@"%@",@"测试分享"] descr:[NSString stringWithFormat:@"%@",@"测试分享"] thumImage:imageData];
        //设置网页地址
        shareObject.webpageUrl = @"http://m.weshare12.com";
        //分享消息对象设置分享内容对象
        messageObject.shareObject = shareObject;
        
        //调用分享接口
        [[UMSocialManager defaultManager] shareToPlatform:UMSocialPlatformType_Qzone messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                UMSocialLogInfo(@"************Share fail with error %@*********",error);
            }else{
                if ([data isKindOfClass:[UMSocialShareResponse class]]) {
                    NSLog(@"QQ空间分享成功！");
                    
                }else{
                    UMSocialLogInfo(@"response data is %@",data);
                }
            }
        }];
    }
}

//压缩图片
-(UIImage *)imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth {
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = defineWidth;
    CGFloat targetHeight = (targetWidth / width) * height;
    UIGraphicsBeginImageContext(CGSizeMake(targetWidth, targetHeight));
    [sourceImage drawInRect:CGRectMake(0,0,targetWidth, targetHeight)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end
